package com.talent.jiwen_teacher.my;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.course.OrderAppealActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.AppealListResult;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;

public class AppealListActivity extends BaseActivity {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;

    private int startIndex = 1;
    private List<AppealListResult.Appeal> dataList;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_appeal_list;
    }

    @Override
    public String titleName() {
        return "申诉列表";
    }

    @Override
    protected void initData() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        recycleView.setAdapter(new CommonAdapter<AppealListResult.Appeal>(this , R.layout.item_appeal_list , dataList){
            @Override
            protected void convert(ViewHolder holder, AppealListResult.Appeal appeal, int position) {

            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                startIndex = 1;
                getAppealList();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                startIndex ++;
                getAppealList();
            }
        });

        refreshLayout.autoRefresh();

    }

    /**获取申诉列表
     *
     */
    private void getAppealList(){
        Map<String, String> map = new HashMap<>();
        map.put("teacherId" , SPUtil.getUserId());
        map.put("startIndex" , startIndex + "");

        Observable observable = ApiFactory.getEducationApiSingleton().getAppealList(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<AppealListResult>(AppealListActivity.this) {
            @Override
            protected void _onNext(AppealListResult result) {
                if(startIndex == 1){
                    dataList.clear();
                    dataList.addAll(result.getAppealList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishRefresh();
                }else{

                    dataList.addAll(result.getAppealList());
                    recycleView.getAdapter().notifyDataSetChanged();
                    refreshLayout.finishLoadMore();

                    if (result.getAppealList().size() < 10 ){
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }

                if (Validators.isEmpty(dataList)){
                    noDataIv.setVisibility(View.VISIBLE);
                }else{
                    noDataIv.setVisibility(View.GONE);
                }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
                refreshLayout.finishRefresh();
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }
}
